<?php

namespace app\model\user;

use app\base\BaseModel;

/**
 * 模型-UserSettlementModel
 * Class UserSettlement
 * @package app\model
 */
class UserSettlementModel extends BaseModel
{
    protected $name = 'user_settlement';

    /**
     * 范围查询-status
     * @param $query
     * @param $user_id
     */
    public function scopeUserid($query, $user_id = '')
    {
        if (!empty($user_id)) {
            $query->where('user_id', '=', $user_id);
        }
    }

    /**
     * 获取分页列表
     * @param $use
     * @param $user_id
     * @param $current_page
     * @param $list_rows
     * @param string $order
     * @return array
     */
    public function getPageData($user_id = '', $current_page, $list_rows, $order = "add_time desc")
    {

        $dataList = $this
            ->field('*')
            ->userid($user_id)
            ->order($order)
            ->paginate(['page' => $current_page, 'list_rows' => $list_rows], false);
        $current = $dataList->currentPage();
        $total = $dataList->total();
        $limit = $dataList->listRows();
        $ret = ['total' => $total, 'current' => $current, 'limit' => $limit, 'dataList' => $dataList->items()];
        return $ret;
    }

    /**
     * 结算记录添加
     * @param $user_id
     * @param $day_sum
     * @param string $day
     * @param $profit_sum
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function addSettlement($user_id, $day_sum, $day = '', $profit_sum = 0)
    {
        $day_arr = explode("-", $day);
        $year = $day_arr[0];
        $month = $day_arr[1];
        $day = $day_arr[2];
        $map = [
            'user_id' => $user_id,
        ];
        $temp = self::where($map)->field('sum,profit_sum,year,month,day')->order('add_time DESC')->find();
        if (!empty($temp)) {
            if (intval($year) == intval($temp['year']) && intval($month) == intval($temp['month']) && intval($day) == intval($temp['day'])) {
                $sum = $day_sum + $temp['sum'];
                $profit_sum_temp = $profit_sum + $temp['profit_sum'];
                $up_data = [
                    'day_sum' => $day_sum,
                    'sum' => $sum,
                    'profit_sum' => $profit_sum_temp,
                ];
                $maps = [
                    'user_id' => $user_id,
                    'year' => $year,
                    'month' => $month,
                    'day' => $day,
                ];
                $ret = self::where($maps)->update($up_data);
                if ($ret == false) {
                    return false;
                }
                if (!empty($profit_sum)) {
                    //正向商城VIP结算
                    UserModel::divideForwardEarnings($user_id, $profit_sum);
                    //正向商城 团队长结算
                    UserModel::divideForwardTeamEarnings($user_id, $profit_sum);
                }
                return true;
            }
            $bean_temp = $temp['sum'];
        } else {
            $bean_temp = 0;
        }
        $sum = $day_sum + $bean_temp;
        $in_data = [
            'user_id' => $user_id,
            'day_sum' => $day_sum,
            'sum' => $sum,
            'profit_sum' => $profit_sum,
            'year' => $year,
            'month' => $month,
            'day' => $day,
            'status' => 2,
            'add_time' => time()
        ];
        $ret = self::insert($in_data);
        if ($ret == false) {
            return false;
        }
        if (!empty($profit_sum)) {
            //正向商城VIP结算
            UserModel::divideForwardEarnings($user_id, $profit_sum);
            //正向商城 团队长结算
            UserModel::divideForwardTeamEarnings($user_id, $profit_sum);
        }
        return true;
    }

    /**
     * 结算记录添加
     * @param $user_id
     * @param string $day
     * @param $day_bean
     * @param $profit_bean
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function addSettlementBean($user_id, $day_bean, $day = '', $profit_bean = 0)
    {
        $day_arr = explode("-", $day);
        $year = $day_arr[0];
        $month = $day_arr[1];
        $day = $day_arr[2];
        $map = [
            'user_id' => $user_id,
        ];
        $temp = self::where($map)->field('bean,profit_bean,year,month,day')->order('add_time DESC')->find();
        if (!empty($temp)) {
            if (intval($year) == intval($temp['year']) && intval($month) == intval($temp['month']) && intval($day) == intval($temp['day'])) {
                $bean = $day_bean + $temp['bean'];
                $profit_bean_temp = $profit_bean + $temp['profit_bean'];
                $up_data = [
                    'bean' => $bean,
                    'day_bean' => $day_bean,
                    'profit_bean' => $profit_bean_temp
                ];
                $map_s = [
                    'user_id' => $user_id,
                    'year' => $year,
                    'month' => $month,
                    'day' => $day,
                ];
                $ret = self::where($map_s)->update($up_data);
                if ($ret == false) {
                    return false;
                }
                if (!empty($profit_bean)) {
                    //负向商城VIP结算
                    UserModel::divideNegativeEarnings($user_id, $profit_bean);
                    //负向商城团队长结算
                    UserModel::divideNegativeTeamEarnings($user_id, $profit_bean);
                }
                return true;
            }
            $bean_temp = $temp['bean'];
        } else {
            $bean_temp = 0;
        }
        $bean = $day_bean + $bean_temp;
        $in_data = [
            'user_id' => $user_id,
            'bean' => $bean,
            'profit_bean' => $profit_bean,
            'day_bean' => $day_bean,
            'year' => $year,
            'month' => $month,
            'day' => $day,
            'status' => 2,
            'add_time' => time()
        ];
        $ret = self::insert($in_data);
        if ($ret == false) {
            return false;
        }
        if (!empty($profit_bean)) {
            //负向商城VIP结算
            UserModel::divideNegativeEarnings($user_id, $profit_bean);
            //负向商城团队长结算
            UserModel::divideNegativeTeamEarnings($user_id, $profit_bean);
        }
        return true;
    }

    /**
     * 获取统计数据
     * @param array $user_ids
     * @param int $lose
     * @return float
     */
    public static function getSumCount($user_ids = [], $lose = 0)
    {
        if (empty($lose)) {
            return self::whereIn('user_id', $user_ids)->sum('day_sum');
        } else {
            return self::whereIn('user_id', $user_ids)->sum('day_bean');
        }
    }
}